let AudioManager = require('AudioManager')
let Main = require('Main')
let WX = require('WX')

cc.Class({
  extends: cc.Component,

  properties: {},

  onLoad () {},

  onEnable () {
    this.flag = 1
    this.isStoping = false
    this.RigidBody = this.node.getComponent(cc.RigidBody)
    this.carPartical = this.node.getChildByName('carPartical')
    this.onToggle()
  },

  start () {},

  setPoint (point, chapterLevel, speedIncrementer, speedIncreaseRate) {
    this.point = point

    this.node._code = this.point._code

    let startX = point.x[Math.floor(point.x.length * Math.random())]
    let startY = point.y[Math.floor(point.y.length * Math.random())]

    this.node.x = startX
    this.node.y = startY

    this.node._iniRotation = this.point.rota
    let speed = 180 + speedIncrementer
    if(speedIncreaseRate){
      speed *= speedIncreaseRate;
    }
    let carSpeedScale = 1 + chapterLevel * 0.001 + 0.2 * Math.random()
    if (carSpeedScale > 1.5) carSpeedScale = 1.5

    let r = (Math.PI / 180) * (180 - this.point.rota)
    this.vx = carSpeedScale * speed * Math.cos(r)
    this.vy = carSpeedScale * speed * Math.sin(r)

    let n = 13
    this.endDot = {
      x: startX + n * this.vx,
      y: startY + n * this.vy
    }
  },

  move () {
    // console.log("Car-move");
    this.carPartical.getComponent(cc.ParticleSystem).resetSystem()
    this.carPartical.active = true
    this.carPartical.x = this.node.width / 2.35
    this.node.rotation = this.point.rota
    this.RigidBody.linearDamping = 0
    this.RigidBody.angularDamping = 1000
    this.RigidBody.linearVelocity = cc.v2(this.vx, this.vy)
  },

  onToggle () {
    // console.log("Car-onToggle");
    if (this.node._isGoods) return
    if (!this.hadOnToggole) {
      this.hadOnToggole = true
      this.node.on(
        cc.Node.EventType.TOUCH_START,
        () => {
          // console.log("Car-TOUCH_START");
          WX.vibrate(false)
          this.shine()
          if (this.isStoping) {
            this.node.rotation = this.node._iniRotation
            this.isStoping = false
            this.RigidBody.linearDamping = 0
            this.speedUp = 1
          } else {
            this.isStoping = true
            this.RigidBody.linearDamping = 15
          }
        },
        this
      )
    }
  },

  shine () {
    this.node.theCar.color = new cc.Color(180, 180, 180)
    this.node.scale = 1.05

    setTimeout(()=>{
      this.node.scale = 1.0
      this.node.theCar.color = new cc.Color(255, 255, 255)
    },100);
  },

  // 只在两个碰撞体开始接触时被调用一次
  onBeginContact (contact, selfCollider, otherCollider) {
    // console.log("car-onBeginContact");
    let over = (r1, r2) => {
      AudioManager.instance.play('crash')
      WX.vibrate(true)
      selfCollider.node.getComponent('Car').shine()
      Main.instance.makeCollision(r1, r2)
      Main.instance.isGameStart = false
      Main.instance.slowDownAllCars()
      setTimeout(()=>{
        Main.instance.gameOver()
      }, 2000);
    }

    if (!Main.instance.isGameStart) return
    let otherCarType = otherCollider.node._carType
    if (otherCarType === 'ball') return

    let r1 = selfCollider.node.getComponent('Car').point.rota

    let r2

    if (otherCarType === 'fireTruck' || otherCarType === 'train') {
      over(r1, r1 + 90)
    } else {
      r2 = otherCollider.node.getComponent('Car').point.rota
      if (r1 == r2) {
        this.isStoping = true
      } else {
        this.RigidBody.linearDamping = 10
        over(r1, r2)
      }
    }
  },

  slowDown () {
    this.RigidBody.linearDamping = 1
    this.RigidBody.angularDamping = 1
  },

  // 只在两个碰撞体结束接触时被调用一次
  onEndContact: function (contact, selfCollider, otherCollider) {
    // console.log("car-onEndContact");
  },

  // 每次将要处理碰撞体接触逻辑时被调用
  onPreSolve: function (contact, selfCollider, otherCollider) {
    // console.log("car-onPreSolve");
  },

  // 每次处理完碰撞体接触逻辑时被调用
  onPostSolve: function (contact, selfCollider, otherCollider) {
    // console.log("car-onPostSolve");
  },
  update (dt) {
    if (this.speedUp <= 5) {
      let d = this.speedUp / 5
      this.speedUp++
      this.RigidBody.linearVelocity = cc.v2(d * this.vx, d * this.vy)
    }

    this.flag++
    if (this.flag % 20 == 0 && this.endDot) {
      // 可以根据方向再作出优化
      let fitX = false

      let fitY = false
      if (this.vx > 0) {
        if (this.endDot.x <= this.node.x) {
          fitX = true
        }
      } else {
        if (this.endDot.x >= this.node.x) {
          fitX = true
        }
      }

      if (this.vy > 0) {
        if (this.endDot.y <= this.node.y) {
          fitY = true
        }
      } else {
        if (this.endDot.y >= this.node.y) {
          fitY = true
        }
      }

      if (fitX && fitY) {
        // console.log("fitX && fitY");
        // console.log(this.node.x, this.node.y)
        if(Main.instance.carPool){
          Main.instance.carPool.put(this.node)
        }
      }
    }
  }
})
